/**
 * Created by zhaocong on 2016/7/21.
 * 基于jQuery.js 日历插件，用于生成日历模板
 */
(function ($) {
  "use strict";
  $.fn.extend({
    cccal: function (o) {
      var self = this,
          datetime = new Date(o.date) || new Date();

      var f = function (a, g) {
        var g = g || 'y/m/d',
            date = new Date(a),
            y = date.getFullYear(),
            m = date.getMonth() + 1,
            d = date.getDate(),
            m = m < 10 ? '0' + m : m,
            d = d < 10 ? '0' + d : d;
        return g.replace(/y/, y).replace(/m/, m).replace(/d/, d);
      };

      var ini = function (date) {
        var prevMon = new Date(date.getFullYear(), date.getMonth(), 0), // 上个月
            thisMon = new Date(date.getFullYear(), date.getMonth() + 1, 0), // 本月
            nextMon = new Date(date.getFullYear(), date.getMonth() + 2, 0), // 下个月
            tL = thisMon.getDate(), // 本月最大天数
            pb = prevMon.getDay(), // 上个月最后一天星期几
            ph = prevMon.getDate() - pb - 1, // 上个月最后几天
            tFday = pb + 1, // 这个月的第一天星期几
            col = pb < 6 ? Math.ceil((tL + tFday) / 7) : Math.ceil((tL + tFday) / 7) - 1, // 表格行数
            weekName = ['日', '一', '二', '三', '四', '五', '六'],
            week = [],
            table = [];
        var k = 0, n = 0, i = 0, j = 0;

        table.push('<table class="cccal-table">');
        // 标题
        table.push('<caption><label class="fa fa-arrow-circle-left pull-left j_cleft"></label>' +
            f(date, 'y年m月') + '<label class="fa fa-arrow-circle-right pull-right j_cright"></label></caption>');
        // 星期
        week.push('<thead><tr>');
        for (i = 0; i < 7; i++) {
          week.push('<th>' + weekName[i] + '</th>');
        }
        week.push('</tr></thead>');
        table.push(week.join(''));
        // 日历
        table.push('<tbody>');
        for (i = 0; i < col; i++) { // 循环行数
          var row = [];
          row.push('<tr>');
          for (j = 0; j < 7; j++) { // 星期
            var day = {};
            var tag = '';
            if (i == 0 && pb < 6 && j <= pb) { // 上个月
              day.i = ++ph;
              day.dis = true;
              day.id = f(prevMon.setDate(day.i));
            } else {
              n++;
              if (n <= tL) { // 这个月
                day.i = n;
                day.id = f(thisMon.setDate(day.i));
              } else { // 下个月
                day.i = ++k;
                day.dis = true;
                day.id = f(nextMon.setDate(day.i));
              }
            }

            if (o.tag[day.id]) tag = o.tag[day.id]; // 注入变量
            if (day.dis) {
              row.push('<td class="text-gray"><p data-date="' + day.id + '">' +
                  day.i + '</p><label>' + tag + '</label></td>');
            } else {
              row.push('<td><p data-date="' + day.id + '">' +
                  day.i + '</p><label>' + tag + '</label></td>');
            }
          }
          row.push('</tr>');
          table.push(row.join(''));
        }
        table.push('</tbody></table>');
        self.html(table.join(''));
      };

      ini(datetime);

      $(this).on('click', '.j_cleft', function () {
        datetime = new Date(datetime.setMonth(datetime.getMonth() - 1));
        ini(datetime);
      });
      $(this).on('click', '.j_cright', function () {
        datetime = new Date(datetime.setMonth(datetime.getMonth() + 1));
        ini(datetime);
      });

      return this;
    }
  });
})(jQuery);